package org.genntii.smuserserver.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import org.genntii.smdomain.entity.User;

import java.time.LocalDate;
import java.time.LocalDateTime;

@Mapper
public interface UserMapper extends BaseMapper<User> {

    @Update("update User set follower_count = #{follower}, subscriber_count = #{subscriber} where id = #{userId}")
    void updateFollowerSubscriber(@Param("follower")Long follower,
                                  @Param("subscriber")Long subscriber,
                                  @Param("userId")Long userId);

    @Select("select nickname from User where id = #{id}")
    String getNickNameById(@Param("id")Long id);

    void updateUser(String nickname, String description, String phone, String avatar, LocalDateTime birthday, Byte gender, String backgroundImage, Long id);

    @Select("select * from User where username=#{username}")
    User selectUserByUsername(@Param("username") String username);

    @Update("update User set salt = #{salt} where id=#{id}")
    void insertSaltById(@Param("id") String id,@Param("salt") String salt);

    /**
     * 验证邮箱是否已被使用注册
     * @param email 待注册邮箱
     * @return 未被使用返回 0
     */
    @Select("SELECT COUNT(*) FROM User WHERE email = #{email};")
    int emailExists(@Param("email") String email);

    /**
     * 在插入UserDetail时 插入邮箱
     * @param email 注册邮箱
     * @param id 用户id
     */
    @Update("update User set email = #{email} where id = #{id};")
    void registerByEmail(@Param("email") String email, @Param("id") Long id);

    @Update("update User set login_time = #{loginTime} where id = #{id};")
    void updateLoginTime(@Param("loginTime")LocalDateTime loginTime, @Param("id") Long id);

    @Update("update User set steamid = #{steamid} where id = #{id}")
    void updateSteamId(@Param("steamid") Long steamid, @Param("id") Long id);

    @Select("select count(*) from User where username = #{username};")
    int usernameIsExist(@Param("username")String username);

    @Select("select count(*) from User where id = #{id};")
    int userIdExists(@Param("id")Long id);

    @Update("update User set nickname = #{nickname}, description = #{description}," +
            "email = #{email}, avatar = #{avatar}, birthday = #{birthday}, gender = #{gender}, background_ima   ge = #{backgroundImage} " +
            "where id = #{userId}")
    void updateUserMessage(@Param("nickname") String nickname,
                           @Param("description") String  description,
                           @Param("email") String email,
                           @Param("avatar") String avatar,
                           @Param("birthday")LocalDate birthday,
                           @Param("gender") Byte gender,
                           @Param("backgroundImage")String backgroundImage,
                           @Param("userId")Long userId);
}
                                      